<template>
	<view class="goshop-buy-container">
		<u-toast ref="uToast"></u-toast>
		<view class="goshop-buy-address" v-if="addressData" @tap="onSelectAddress()">
			<view class="goshop-buy-address-left">
				<view class="row-x">
					<view>
						{{addressData.consignee}}
					</view>
					<view class="u-m-l-20">
						{{addressData.mobile}}
					</view>
					<view class="def-address" v-if="addressData.defaultStatus === 1">{{defaultText}}</view>
				</view>
				<view class="row-x u-m-t-20">
							<view v-if="locale ==='zh-Hans'">{{addressData.province}}</view>
							<view v-if="locale ==='zh-Hans'">-{{addressData.city}}</view>
							<view v-if="locale ==='zh-Hans'">-{{addressData.area}}</view>
							<view class="u-m-l-10">{{addressData.address}}</view>
				</view>
			</view>
			<view class="goshop-buy-address-right">
				<view class="iconfont iconfont-right_arrow icon-bg-color"></view>
			</view>
		</view>
		<view v-else class="goshop-buy-address-add" @tap="onSelectAddress()">
			<view class="goshop-buy-address-left">
				<view class="iconfont iconfont-area"></view>
				<view class="u-m-l-20">{{addaddressText}}</view>
			</view>
			<view class="goshop-buy-address-right">
				<view class="iconfont iconfont-right_arrow icon-bg-color"></view>
			</view>
		</view>
		<view class="goshop-buy-shop">
			<view class="goshop-buy-shop-head">
				<view>
					<view class="iconfont iconfont-shop"></view>
				</view>
				<view class="u-m-l-20">
					{{form.merchantName}}
				</view>
			</view>
			<view class="goshop-buy-shop-body">
				<view class="goshop-buy-shop-body-left">
					<image class="goshop-img-62" :src="form.goodThumbnail"></image>
				</view>
				<view class="goshop-buy-shop-body-right">
					<view class="goshop-buy-shop-body-right-desc">
						<view>{{form.goodName}}</view>
						<view class="u-m-t-20">{{moneyText}}{{form.goodPrice}}</view>
					</view>
					<view>
						<u-number-box v-model="form.goodNum" :min="1" :max="form.goodStock"></u-number-box>
					</view>
				</view>
			</view>
		</view>
		<view class="goshop-buy-remark">
			<view>{{remarkText}}</view>
			<view class="u-m-t-10">
				<u--textarea confirmType="done" v-model="form.msg"  :placeholder="fillremarkText" ></u--textarea>
			</view>
		</view>
		<view class="goshop-buy-action">
			<view class="row-x">
				<view>{{realpaymentText}}</view>
				<view>{{moneyText}}{{form.payment}}</view>
			</view>
			<view>
				<u-button shape="circle" type="error" @click="submit" size="small" :text="confirmpurchaseText"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {defAddress,preOrder,createOrder,addressInfo} from '@/common/http.api.js'
	import {isEmpty} from '@/utils/utils';
	export default {
		data() {
			return {
				addressData:'',
				form:{
					merchantId:'',
					addressId:'',
					goodStock:1,
					goodNum:1,
					goodId:'',
					msg:'',
					goodName:'',
					goodPrice:'',
					merchantName:'',
					goodThumbnail:'',
				},
				addressnotemptyText:'',
				remarkText:'',
				fillremarkText:'',
				confirmpurchaseText:'',
				merchantidnotemptyText:'',
				failText:'',
				addaddressText:'',
				realpaymentText:'',
				defaultText:'',
				goodidnotemptyText:'',
				successText:'',
				moneyText:'',
				locale:'',
			};
		},
		async onLoad(e){
			this.form.goodId = e.gid;
			this.form.merchantId = e.mid;
			//判断是否有地址ID
			if(this.form.addressId){
				this.getAddressInfo();
			}else{
				await this.getDefAddress();
			}
		},
		onShow(){
			let that = this;
			uni.$on('refreshAddress',function(addressId){
				that.form.addressId = addressId;
				console.log('监听到事件来自 updateData ，携带参数为：' + addressId);
				that.getAddressInfo();
			})	
			this.refresh();
			this.locale = this.$i18n.locale;
		},
		methods:{
			refresh(){
			   this.addressnotemptyText = this.$t('buy.addressnotempty.text');
			   this.remarkText = this.$t('buy.remark.text');
			   this.fillremarkText = this.$t('buy.fillremark.text');
			   this.confirmpurchaseText = this.$t('buy.confirmpurchase.text');
			   this.merchantidnotemptyText = this.$t('buy.merchantidnotempty.text');
			   this.failText = this.$t('buy.fail.text');
			   this.addaddressText = this.$t('buy.addaddress.text');
			   this.realpaymentText = this.$t('buy.realpayment.text');
			   this.defaultText = this.$t('buy.default.text');
			   this.goodidnotemptyText = this.$t('buy.goodidnotempty.text');
			   this.successText = this.$t('buy.success.text');
			   this.moneyText = this.$t('common.money.text');
			},
			onSelectAddress(){
				uni.navigateTo({
					url: '/pages/common/address/address?selectAddressId=' + this.form.addressId + '&mode=select'
				})
			},
			getPreOrder(){
				preOrder(this.form).then(res => {
					if (res.code === 0) {
						this.form = res.data;
					} else {
						uni.hideLoading();
						this.$refs.uToast.show({
							message: res.msg,
							type: 'error',
						})
					}
				});
			},
			getAddressInfo(){
				addressInfo({params: {id:this.form.addressId}}).then(res => {
					if (res.code === 0) {
						if(res.data){
							this.addressData = res.data;
						}
						this.getPreOrder();
					} else {
						uni.hideLoading();
						this.$refs.uToast.show({
							message: res.msg,
							type: 'error',
						})
					}
				});
			},
			getDefAddress(){
				defAddress({custom: {auth: true}}).then(res => {
					if (res.code === 0) {
						if(res.data){
							this.addressData = res.data;
							this.form.addressId = res.data.id;
						}
						this.getPreOrder();
					} else {
						uni.hideLoading();
						this.$refs.uToast.show({
							message: res.msg,
							type: 'error',
						})
					}
				});
			},
			submit(){
				debugger
				let that = this;
				if(isEmpty(this.form.goodId)){
					this.$refs.uToast.show({
						message: that.goodidnotemptyText,
						type: 'error',
					})
					return;
				}
				if(isEmpty(this.form.merchantId)){
					this.$refs.uToast.show({
						message: that.merchantidnotemptyText,
						type: 'error',
					})
					return;
				}
				if(isEmpty(this.form.addressId)){
					this.$refs.uToast.show({
						message: that.addressnotemptyText,
						type: 'error',
					})
					return;
				}
				debugger
				createOrder(this.form).then(res => {
					if (res.code === 0) {
						this.$refs.uToast.show({
							message: that.successText,
							type: 'success'
						})
						uni.hideLoading();
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/common/order/pay?id='+res.data
							});
						}, 1000);
					} else {
						uni.hideLoading();
						uni.showModal({
							showCancel: false,
							title: that.failText,
							content: res.msg,
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.def-address{
	margin-left: 20rpx;
	background-color: $goshop-color-auxiliary-two;
	height: 40rpx;
	width: 80rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 25px;
	color: $goshop-color-auxiliary-one;
	font-size: $goshop-font-size-sm;
}
.goshop-buy-container{
	.goshop-buy-address{
		height: 120rpx;
		margin-top: 20rpx;
		padding: 20rpx;
		background-color: $goshop-color-auxiliary-one;
		display: flex;
		justify-content: space-between;
		.goshop-buy-address-left{
			display: flex;
			flex-direction: column;
		}
		.goshop-buy-address-right{
			display: flex;
			align-items: center;
		}
	}
	.goshop-buy-address-add{
		margin-top: 20rpx;
		padding:30rpx 20rpx;
		background-color: $goshop-color-auxiliary-one;
		display: flex;
		justify-content: space-between;
		.goshop-buy-address-left{
			display: flex;
			align-items: center;
		}
		.goshop-buy-address-right{
			display: flex;
			align-items: center;
		}
	}
	.goshop-buy-shop{
		background-color: $goshop-color-auxiliary-one;
		margin-top: 20rpx;
		.goshop-buy-shop-head{
			display: flex;
			padding: 20rpx;
			justify-content: flex-start;
			align-items: center;
			border-bottom: 2px solid $goshop-text-color-four;
		}
		.goshop-buy-shop-body{
			padding: 20rpx;
			display: flex;
			.goshop-buy-shop-body-left{
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.goshop-buy-shop-body-right{
				flex: 3;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.goshop-buy-shop-body-right-desc{
					padding: 20rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					flex: 3;
				}
			}
		}
	}
	.goshop-buy-remark{
		background-color: $goshop-color-auxiliary-one;
		margin-top: 20rpx;
		padding: 20rpx;
	}
	.goshop-buy-action{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: $goshop-color-auxiliary-one;
		height: 60rpx;
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
}
</style>
